﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ParsecParser;
using ParsecParser.Data;
using ParsecParser.Helpers;
using ParsecParser.Communication;
using ParsecParser.Display;
using ParsecClient.Classes;

namespace ParsecClient
{
    /// <summary>
    /// Interaction logic for RaidDps.xaml
    /// </summary>
    public partial class RaidDps : Window
    {
        public string CurrentName = null;
        public RaidDps()
        {
            InitializeComponent();
            CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, new ExecutedRoutedEventHandler(delegate(object sender, ExecutedRoutedEventArgs args) { this.Close(); })));

        }

        public void BindData(List<RaidDisplayItem> raidDetails, string name)
        {
            Topmost = true;
            CurrentName = name;
            if (raidDetails != null && raidDetails.Count > 0)
            {
                icRaidDps.ItemsSource = raidDetails.Where(x => x.DamageOut > 0).OrderByDescending(x => x.DamageOut).Select(x=>new {
                    x.Name,
                    ThreatBackgroundValue = x.Name == CurrentName ? 1 : 0,
                    x.DpsOut,
                    x.DamageOut
                }).ToList();
            }
            else
            {
                icRaidDps.ItemsSource = (new List<RaidDisplayItem>()).Select(x => new
                {
                    x.Name,
                    ThreatBackgroundValue = x.Name == CurrentName ? 1 : 0,
                    x.DpsOut,
                    x.DamageOut
                }).ToList();
            }
        }
        public double GetBackgroundOpacity()
        {
            return BorderBackground.Opacity;
        }
        public void SetBackgroundOpacity(double opacity)
        {
            BorderBackground.Opacity = opacity;
            BorderSolid.Opacity = opacity;
            BorderBlur.Opacity = opacity;
        }

        public void DragWindow(object sender, MouseButtonEventArgs args)
        {
            try
            {
                this.WindowState = WindowState.Normal;
                DragMove();
                args.Handled = true;
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", string.Format("Error on {0} Pop Out DragWindow", this.Title), ex, null, true);
            }
        }
        private void btnIncreaseOpacity_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (BorderBackground.Opacity < 1)
                {
                    BorderBackground.Opacity += .1;
                    BorderSolid.Opacity += .1;
                    BorderBlur.Opacity += .1;
                }
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", string.Format("Error on {0} Pop Out btnIncreaseOpacity_Click", this.Title), ex, null, true);
            }
        }

        private void btnDecreaseOpacity_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (BorderBackground.Opacity > 0)
                {
                    BorderBackground.Opacity -= .1;
                    BorderSolid.Opacity -= .1;
                    BorderBlur.Opacity -= .1;
                }
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", string.Format("Error on {0} Pop Out btnDecreaseOpacity_Click", this.Title), ex, null, true);
            }
        }

        private void btnIncreaseSize_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.Width < 1000)
                {
                    this.Width += 10;
                }
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", string.Format("Error on {0} Pop Out btnIncreaseSize_Click", this.Title), ex, null, true);
            }
        }

        private void btnDecreaseSize_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.Width > 100)
                {
                    this.Width -= 10;
                }
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", string.Format("Error on {0} Pop Out btnIncreaseSize_Click", this.Title), ex, null, true);
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (Application.Current.MainWindow != null)
                {
                    ((ParsecWindow)Application.Current.MainWindow).btnRaidDps.IsChecked = false;
                }
                this.Hide();
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", string.Format("Error on {0} Pop Out btnIncreaseSize_Click", this.Title), ex, null, true);
            }
        }
    }
}
